using System;
using System.Runtime.InteropServices;

using ScenarioFramework.WorkflowEngine.DataModel.WorkflowProcesses;
using ScenarioFramework.WorkflowEngine.DataModel.WorkflowRelevantData;

namespace ScenarioFramework.WAPI
{
	/// <summary>
	/// Descrizione di riepilogo per wrpWAPIProcess.
	/// </summary>
	public partial class Process
	{
		/// <summary>
		/// Initializes a new instance  of the <see cref="Process"/> class.
		/// </summary>
		private Process()
		{
		}

        /// <summary>
        /// Creates the process instance.
        /// </summary>
        /// <param name="workflowName">Name of the workflow to create.</param>
        /// <returns>Workflow created</returns>
        public static XMLWorkflow CreateInstance(String workflowName)
        {
            Response response = new Response();
            long processId = 0;
            string sProcess = Process.CreateProcessInstance(workflowName, ref processId, ref response);

            if ((response.iCode != 0) || (processId == 0))
            {
                throw new Exception.WAPIException(response.iCode, response.sMsg);
            }

            XMLWorkflow xmlWorkflow = new XMLWorkflow();
            xmlWorkflow.Parse(sProcess);

            return xmlWorkflow;
        }

        /// <summary>
        /// Creates the process instance.
        /// </summary>
        /// <param name="workflowName">Name of the workflow to create.</param>
        /// <returns>Workflow instance created an started</returns>
        public static long StartInstance(String workflowName)
        {
            Response response = new Response();
            long processId = 0;
            string sProcess = Process.CreateProcessInstance(workflowName, ref processId, ref response);

            if ((response.iCode != 0) || (processId == 0))
            {
                throw new Exception.WAPIException(response.iCode, response.sMsg);
            }

            Process.StartInstance(processId);

            return processId;
        }

        /// <summary>
        /// Starts the process instance.
        /// </summary>
        /// <param name="processId">process ID.</param>
        public static void StartInstance(long processId)
        {
            Response response = new Response();

            Process.StartProcessInstance(processId, ref response);

            if (response.iCode != 0)
                throw new Exception.WAPIException(response.iCode, response.sMsg);
        }

        /// <summary>
        /// Starts the process instance.
        /// </summary>
        /// <param name="processId">process ID.</param>
        /// <param name="xmlAttributes">attributes to set</param>
        public static XMLAttributes StartInstance(long processId, XMLAttributes xmlAttributes)
        {
            XMLAttributes xmlAttributesNotSetted = Process.SetAttributes(processId, xmlAttributes);
            Process.StartInstance(processId);

            return xmlAttributesNotSetted;
        }


        public static XMLAttributes SetAttributes(long processId, XMLAttributes xmlAttributes)
        {
            Response response = new Response();

            String sResult = String.Empty;
            sResult = Process.SetProcessInstanceAttributes(xmlAttributes.GetXML(false, 0), processId, ref response);

            if (response.iCode != 0)
                throw new Exception.WAPIException(response.iCode, response.sMsg);

            XMLAttributes xmlAttributesNotSetted = new XMLAttributes();
            xmlAttributesNotSetted.Parse(sResult);

            return xmlAttributesNotSetted;
        }

        public static XMLAttributes GetAttributes(long processId)
        {
            Response response = new Response();

            String sResult = String.Empty;
            sResult = Process.GetProcessInstanceAttributes(ref response, processId);

            if (response.iCode != 0)
                throw new Exception.WAPIException(response.iCode, response.sMsg);

            XMLAttributes xmlAttributes = new XMLAttributes();
            xmlAttributes.Parse(sResult);

            return xmlAttributes;
        }

        public static XMLReports GetReport(long processId)
        {
            Response response = new Response();
            String sResult = String.Empty;
            
            sResult = Process.GetReports(processId, ref response);

            if (response.iCode != 0)
                throw new Exception.WAPIException(response);

            XMLReports xmlReports = new XMLReports();
            xmlReports.Parse(sResult);

            return xmlReports;
        }
    }
}
